﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace QLNHK
{
     [XmlRoot("Root")]
    public class ConfigNHK
    {
         public static ConfigNHK Install = null;
         public int ThanhPho { get; set; }
         public int QuanHuyen { get; set; }
         public int XaPhuong { get; set; }
         public string CapSuDung { get; set; }
         public string MaKichHoat { get; set; }

         public ConfigNHK()
         { }

         public static string Serializer(object obj, Encoding encoding, bool removeDeclartion, bool removeNameSpace)
        {

            XmlWriterSettings xmlWS = new XmlWriterSettings();
            if (removeDeclartion)
                xmlWS.OmitXmlDeclaration = true;
            xmlWS.Encoding = encoding;

            // xmlWS.Encoding = new UTF7Encoding(true);

            UtfStringWriter sWriter = new UtfStringWriter(encoding);
            using (XmlWriter xmlWriter = XmlWriter.Create(sWriter, xmlWS))
            {
                XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                if (removeNameSpace)
                    namespaces.Add("", "");//Remove NameSpace
                else
                    namespaces.Add("dt", "urn:schemas-microsoft-com:datatypes");// Add Namespace
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(xmlWriter, obj, namespaces);
                return sWriter.ToString();
            }
        }
         public static T Deserialize<T>(string sfmtObjectXml)
         {
             if (string.IsNullOrEmpty(sfmtObjectXml)) throw new ArgumentNullException("sfmtObjectXml không được rỗng");
             using (StringReader reader = new StringReader(sfmtObjectXml))
             {
                 XmlSerializer serializer = new XmlSerializer(typeof(T));
                 return (T)serializer.Deserialize(reader);
             }
         }
         public static void SaveConfig(ConfigNHK o)
        {
            string fileName = "NHK.config";
            TextWriter tw = new StreamWriter(fileName);
            string text = Serializer(o,Encoding.UTF8,false,false);
            tw.Write(text);
            tw.Close();
        }
         static ConfigNHK()
        {
            string fileName = "NHK.config";
            try
            {
                if (File.Exists(fileName))
                {
                    TextReader tr = new StreamReader(fileName);
                    string text = tr.ReadToEnd();
                    Install = Deserialize<ConfigNHK>(text);
                    tr.Close();
                }
                else throw new Exception("Chưa cấu hình hệ thống");
            }
            catch (Exception ex)
            {
                Install = new ConfigNHK();
                SaveConfig(Install);

            }
        }
    }
    class UtfStringWriter : StringWriter
    {
        Encoding _enCoding = Encoding.UTF8;
        public UtfStringWriter(Encoding encoding)
        {
            _enCoding = encoding;
        }
        public override Encoding Encoding
        {
            get { return _enCoding; }
        }
    }
}
